<template>
  <view>
    
    <!-- 搜索 -->
    <view class="search">
      <my-search @click="toSearch"></my-search>
    </view>
    
    
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item  v-for="(item,index) in swipList" :key="index">
        <navigator :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
        <image :src="item.image_src" mode="widthFix"></image>
        </navigator>
      </swiper-item>
    </swiper>
    
    <!-- 导航 -->
    <view class="navi">
      <view class="navi-item" v-for="(item,index) in cateList" :key="index" @click="toCategory(item)">
        <img :src="item.image_src">
      </view>
    </view>
    
    <!-- 楼层 -->
    <view class="floor" v-for="(item,index) in floorList" :key="index" >
      <!-- 标题 -->
      <view class="floor-titImg">
        <img :src="item.floor_title.image_src">
      </view>
      
      <!-- 内容 -->
      <view class="ImgBox">
        <!-- 左图 -->
        <navigator class="floor-conLeftImg" :url="item.product_list[0].url">
          <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width+'rpx'}" mode="widthFix"></image>
        </navigator>
        
        <!-- 右图 -->
        <view class="right-img-box">
          <navigator class="conRightImg" v-for="(it,i) in item.product_list" :key="i" v-if="i!=0" :url="it.url">
            <image :src="it.image_src" mode="widthFix" :style="{width:item.product_list[i].image_width+'rpx'}"></image>
          </navigator>
        </view>
      </view>
    </view>
    
    
  </view>
</template>

<script>
  import badge from "@/mixins/tabbar.js"
  
  export default {
    mixins:[badge],
    data() {
      return {
        swipList:[],//存储轮播图
        cateList:[],//存储分类数据
        floorList:[],//存储楼层数据
      };
    },
    onLoad() {
      this.getSwiperList()//获取轮播图数据
      this.getCateNavi()//获取分类导航数据
      this.getFloor()//获取楼层数据      
    },
    methods:{
      async getSwiperList(){
        const {data:res} = await uni.$http.get("/api/public/v1/home/swiperdata")//{data:res} 只取data的值，命名为res        
        if(res.meta.status==200){
          this.swipList = res.message
        }else{
          return uni.showMsg()//返回封装的showToast方法
        }
      },
      
      async getCateNavi(){//async 声明是异步方法  await等待异步方法执行完成
        const {data:res} = await uni.$http.get("/api/public/v1/home/catitems")        
        if(res.meta.status==200){
          this.cateList = res.message
        }else{
          return uni.showMsg()
        }
      },
      
      toCategory(item){
        if(item.name == "分类"){
          uni.switchTab({//switchTab跳转到tabBar页面
            url:"../cate/cate"
          })
        }
      },
      
      async getFloor(){//获取楼层数据
        const {data:res} = await uni.$http.get("/api/public/v1/home/floordata")
        if(res.meta.status ==200){
          //数据处理，将url改为本地地址
          res.message.forEach(floor=>{
            floor.product_list.forEach(pro=>{
              pro.url = "/subpkg/goods-list/goods-list?"+pro.navigator_url.split('?')[1]
            })
          })
          this.floorList = res.message
        }else{
          return uni.showMsg()
        }
      },
      toSearch(){
        uni.navigateTo({
          url:"/subpkg/search-page/search-page"
        })
      },
      }
  }
</script>

<style lang="scss">
  .search{
    position: sticky;
    top: 0;
    z-index: 1;
  }
  swiper{//轮播
    height: 330rpx;
    swiper-item,
    navigator,
    image{
      width: 100%;
    }
  }
  .navi{//导航
    display: flex;
    justify-content: space-around;
    margin: 10rpx 0rpx;
    .navi-item{
      width: 140rpx;
      height: 150rpx;
      img{
        width: 100%;
        height: 100%;
      }
    }
  }
  .floor{//楼层图
    // border: 1px solid red;
    margin:15rpx;
    .floor-titImg img{
      width: 700rpx;
      height: 70rpx;
    }    
    .ImgBox{
      display: flex;      
      // border: 1px solid black;
      width: 100%;
      .floor-conLeftImg{
        margin-right: 20rpx;
      }
      .right-img-box{
        display: flex;
        flex-wrap: wrap;
      }
    }
  }
</style>
